package com.example.studentmanager.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.studentmanager.R;
import com.example.studentmanager.db.StudentManager;

public class SettingActivity extends AppCompatActivity {
    private Switch switchManager;
    private View llManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
    }

    private void initView() {
        switchManager = findViewById(R.id.switchManager);
        llManager = findViewById(R.id.llManager);
        switchManager.setChecked(true);
        switchManager.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                updateManagerUI();
            }
        });

        findViewById(R.id.tvAddStudent).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SettingActivity.this, AddStudentActivity.class));
            }
        });

        findViewById(R.id.btLogout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StudentManager.logout()) {
                    startActivity(new Intent(SettingActivity.this, LoginActivity.class));
                    finish();
                } else {
                    Toast.makeText(SettingActivity.this, "退出登录失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void updateManagerUI() {
        if (switchManager.isChecked()) {
            llManager.setVisibility(View.VISIBLE);
        } else {
            llManager.setVisibility(View.GONE);
        }
    }
}
